//
//  AddViewController.m
//  通讯录
//
//  Created by mafeng on 16/8/6.
//  Copyright © 2016年 mafeng. All rights reserved.
//

#import "AddViewController.h"

@interface AddViewController ()<UIImagePickerControllerDelegate, UIActionSheetDelegate, UINavigationControllerDelegate>

@end

@implementation AddViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    [_sureButton addTarget:self action:@selector(sureAction) forControlEvents:UIControlEventTouchUpInside];
    
    [_headButton addTarget:self action:@selector(headButtonAction) forControlEvents:UIControlEventTouchUpInside];
}

- (void)headButtonAction {
    UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"选择图片" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"从相册中选择" otherButtonTitles:nil, nil];
    [sheet showInView:self.view];
    
    
}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex == 0) {
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    picker.allowsEditing = YES;
    [self presentViewController:picker animated:YES completion:^{
        
        
    }];
    }
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {
    // 手动消失
    [picker dismissViewControllerAnimated:YES completion:^{
       // 将choose图片取出来
        UIImage *image = info[UIImagePickerControllerOriginalImage];
        NSLog(@"%@", image);
        // 设置button的背景图片
        [self.headButton setBackgroundImage:image forState:UIControlStateNormal];
    }];
    
}
// 点击确定按钮时触发
- (void)sureAction {
    [self.navigationController popToRootViewControllerAnimated:YES];
    
    
    
    NSData *imageData = UIImageJPEGRepresentation(self.headButton.currentBackgroundImage, 0.5);
    NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:[self.nameTextField.text description], @"name", self.ageTextField.text, @"age", self.phoneTextField.text, @"phoneNumber", [self.sexTextField.text description], @"gender", imageData, @"headImageData", nil];
  
    
    [self.delegate passValue:dict];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
